<?php
/*
说明 : 这是注册 ajax 请求文件
*/
include '../../configs/global.php';
$data = $_POST;
$data = json_decode($data['data']);
$id = $_POST['cid'];

// 定义一个返回数组
$ajax_res = array();
//查询数据库，比对出成绩
$sql = $sql = "select resources.resourcesinfo,questions.*,curriculum.cname  from resources,questions,curriculum where   questions.cid='$id' and resources.qid=questions.id and curriculum.id='$id'";
$rows = $pdo->getAll($sql);

$flog = 0;





foreach ($data as  $da) {
    if (is_null($da)) {
        continue;
    }

    if ($da->data->value == "") {
        continue;
    }


    foreach ($rows as $val) {
        if ($val['id'] == $da->cid) {
            if ($da->data->name == '多选') {
                if ($val['resourcesinfo'] == $da->data->value) {
                    $flog += 5;
                } else if (strpos($val['resourcesinfo'], $da->data->value) !== false) {
                    $flog += 3;
                }
                break;
            } else {
                if (strpos($val['resourcesinfo'], $da->data->value) !== false) {
                    $flog += 5;
                }
                break;
            }
        }
    }
}




//保存成绩
$stuendanme = @$_SESSION['username'];
$cname = @$rows[0]['cname'];
$cid = @$rows[0]['cid'];
$init = "insert into achievement(fraction,subject,cid,student) values('$flog','$cname','cid','$stuendanme')";

$r1 = $pdo->exec($init);
if ($r1) {
    $ajax_res['code'] = 200;
    $ajax_res['info'] = $flog;
} else {
    $ajax_res['code'] = 300;
    $ajax_res['info'] = '分数获取失败失败,请稍后再试！';
}
echo json_encode($ajax_res);
